/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jsf.rest;

import com.jsf.entity.User;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author gang
 */
@Stateless
@Path("com.jsf.entity.user")
public class UserFacadeREST extends AbstractFacade<User> {

    @PersistenceContext(unitName = "com.jsf_FastJSF_war_1.0-SNAPSHOTPU")
    private EntityManager em;

    public UserFacadeREST() {
        super(User.class);
    }

    @POST
    @Override
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void create(User entity) {
        super.create(entity);
    }

    @PUT
    @Path("{id}")
    @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public void edit(@PathParam("id") String id, User entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") String id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public User find(@PathParam("id") String id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<User> findAll() {
        return super.findAll();
    }
    
//     @GET
//    @Path("findSome/{type}/{fs}")
//    @Produces(MediaType.APPLICATION_JSON)
//    public List<ConstorageLocation> findByUserName(@PathParam("type") String findType,@PathParam("fs") String findString) {
//        if (EvalUtil.isEmpty(findType)&&EvalUtil.isEmpty(findType)) {
//            return null;
//        }
//        String hql = "select c from ConstorageLocation c where c.locationName = :findString";
//        switch(findType){
//            case "storageid":
//                int intStorageId = Integer.parseInt(findString);
//                hql = "select c from ConstorageLocation c where c.storageid = :findString";
//                  return em.createQuery(hql)
//                .setParameter("findString", intStorageId)
//                .getResultList();
//
//            case "locationid":
//                int intLocationId = Integer.parseInt(findString);
//                 hql = "select c from ConstorageLocation c where c.locationid like :findString";
//                 return em.createQuery(hql)
//                .setParameter("findString", intLocationId)
//                .getResultList();
//
//            case "location_name":
//                System.out.println("com.yuntuo.dc.dbs.service.ConstorageLocationFacadeREST.findByUserName()");
//                 hql = "select c from ConstorageLocation c where c.locationName like :findString";
//                
//        }
//        return em.createQuery(hql)
//                .setParameter("findString", "%" + findString + "%")
//                .getResultList();
//       
//    }
//    
//    @GET
//    @Path("findSomeNoType/{fs}")
//    @Produces(MediaType.APPLICATION_JSON)
//    public List<ConstorageLocation> findSomeNoType(@PathParam("fs") String findString) {
//        if (EvalUtil.isEmpty(findString)) {
//            return null;
//        }
//        return em.createQuery("select c from ConstorageLocation c where  c.locationName like :findString or c.locationid like :findString or c.storageid like :findString or c.llength like :findString or c.lweigh like :findString or c.lhigh like :findString")
//                .setParameter("findString", "%" + findString + "%")
//                .getResultList();
//    }
//    
//    @GET
//    @Path("findByName/{fs}")
//    @Produces(MediaType.APPLICATION_JSON)
//    public int findByName(@PathParam("fs") String findString) {
//        if (EvalUtil.isEmpty(findString)) {
//            return 0;
//        }
//        try {
//         return ((Long)em.createQuery("select count(c.locationid) from ConstorageLocation c where  c.locationName = :findString")
//                .setParameter("findString", findString)
//                .getSingleResult()).intValue();
//         } catch (NoResultException e) {
//            return 0;
//        }
//    }
//    
//    @GET
//    @Path("findDatasBySortFilter")
//    @Produces(MediaType.APPLICATION_JSON)
//    public List<ConstorageLocation> findDatasBySortFilter(@QueryParam("sortStr") String sortStr,@QueryParam("firstS") int firstS,@QueryParam("maxResultS") int maxResultS,
//            @QueryParam("groupidFtr") String groupidFtr,@QueryParam("nameFtr") String nameFtr,@QueryParam("isactiveFtr") Boolean isactiveFtr,@QueryParam("typeFtr") String typeFtr){
//        System.out.println("com.yuntuo.dc.dbs.remote.ConstorageLocationJC.findDatasBySortFilter()"+"-"+firstS+"-"+maxResultS+"-"+groupidFtr+"-"+nameFtr+"-"+isactiveFtr+"-"+sortStr+"_");
//        Object intLocationId;
//        if(EvalUtil.isEmpty(groupidFtr)){
//            intLocationId = null;
//        }else{
//            intLocationId= (int)Integer.parseInt(groupidFtr);
//        }
//       
//        
//        return em.createQuery("select c from ConstorageLocation c where (c.locationName like :locationName or :locationName is null) and (c.locationid = :locationid or :locationid is null)and (c.isActive=:isactive or :isactive is null)and (c.rtype=:typeFtr or :typeFtr is null)"
//                + ( EvalUtil.isEmpty(sortStr) ? "" : " order by " + sortStr))
//                .setParameter("locationName", EvalUtil.isEmpty(nameFtr) ? null : "%".concat(nameFtr).concat("%"))
//                .setParameter("locationid",intLocationId)
//                .setParameter("isactive", isactiveFtr)
//                .setParameter("typeFtr", typeFtr)
//                .setFirstResult(firstS)
//                .setMaxResults(maxResultS)
//                .getResultList();
//    }
//    
//    
//    @GET
//    @Path("findCountWithLazyMode")
//    @Produces(MediaType.APPLICATION_JSON)
//    public int findCountWithLazyMode(@QueryParam("sortStr") String sortStr,@QueryParam("firstS") int firstS,@QueryParam("maxResultS") int maxResultS,
//            @QueryParam("groupidFtr") String groupidFtr,@QueryParam("nameFtr") String nameFtr,@QueryParam("isactiveFtr") Boolean isactiveFtr,@QueryParam("typeFtr") String typeFtr){
//        System.out.println("com.yuntuo.dc.dbs.remote.ConstorageLocationJC.findDatasBySortFilter()"+"-"+firstS+"-"+maxResultS+"-"+groupidFtr+"-"+nameFtr+"-"+isactiveFtr+"-"+sortStr+"_");
//        Object intLocationId;
//        if(EvalUtil.isEmpty(groupidFtr)){
//            intLocationId = null;
//        }else{
//            intLocationId= (int)Integer.parseInt(groupidFtr);
//        }
//        
//        try {
//            
//            return ((Long)em.createQuery("select count(c.locationid) from ConstorageLocation c where (c.locationName like :locationName or :locationName is null) and (c.locationid = :locationid or :locationid is null)and (c.isActive=:isactive or :isactive is null)and (c.rtype=:typeFtr or :typeFtr is null)"
//                    + ( EvalUtil.isEmpty(sortStr) ? "" : " order by " + sortStr))
//                    .setParameter("locationName", EvalUtil.isEmpty(nameFtr) ? null : "%".concat(nameFtr).concat("%"))
//                    .setParameter("locationid",intLocationId)
//                    .setParameter("isactive", isactiveFtr)
//                    .setParameter("typeFtr", typeFtr)
//                    .getSingleResult()).intValue();
//        } catch (NoResultException e) {
//            return 0;
//        }
//    }

    @GET
    @Path("{from}/{to}")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<User> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces(MediaType.TEXT_PLAIN)
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
    
}
